package com.gec.system.utils;

import com.gec.system.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    public static List<SysMenu> buildTree(List<SysMenu> sysMenus ){
        ArrayList<SysMenu> trees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getParentId().longValue()==0){
                trees.add(findChildren(sysMenu,sysMenus));
            }
        }
        return trees;
    }
    public static SysMenu findChildren(SysMenu sysMenu ,List<SysMenu> treeNodes){
        sysMenu.setChildren(new ArrayList<SysMenu>());
        for (SysMenu treeNode : treeNodes) {
            Long id = sysMenu.getId();
            Long parentId = treeNode.getParentId();
            if (sysMenu.getId()==treeNode.getParentId()){
                if (sysMenu.getChildren()==null){
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(findChildren(treeNode,treeNodes));
            }
        }
        return sysMenu;

    }
}
